package com.ibeeking.found.uaa.c.config.auth.sms;

import com.ibeeking.found.uaa.c.config.auth.token.AbstractCustomTokenGranter;
import com.ibeeking.found.uaa.c.model.ClientUserDetails;
import com.ibeeking.found.uaa.c.model.WxAppletModel;
import com.ibeeking.found.uaa.c.service.IClientUserDetailsService;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

/**
 * @Author ibeeking
 */
public class WxAppletsTokenGranter extends AbstractCustomTokenGranter {

    private static final String GRANT_TYPE = "wx_ma";
    private static final String OPEN_ID = "open_id";

    private final IClientUserDetailsService userDetailsServiceImpl;

    public WxAppletsTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsServiceImpl, OAuth2RequestFactory requestFactory, IClientUserDetailsService userDetailsServiceImpl) {
        super(tokenServices, clientDetailsServiceImpl, requestFactory, GRANT_TYPE);
        this.userDetailsServiceImpl = userDetailsServiceImpl;
    }

    @Override
    protected ClientUserDetails getUserDetails(Map<String, String> parameters) {
        WxAppletModel wxAppletModel = new WxAppletModel();
        wxAppletModel.setOpenId(parameters.get(OPEN_ID));
        return userDetailsServiceImpl.wxAppletsLogin(wxAppletModel);
    }
}
